<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\adminapi\controller\v1\platform\wechat;

use app\adminapi\controller\v1\platform\PlatformController;
use think\App;
use think\Request;
use app\common\model\platform\Platform;
use hiduoke\services\FormBuilder as Form;
use app\common\controller\AdminController;
use app\common\model\platform\PlatformWechat;

/**
 * Class Wechat 微信公众号
 * @package app\adminapi\controller\v1\platform
 * User: liuhongbin
 * Date: 2021-05-25 14:14
 */
class Wechat extends PlatformController
{

    public $level_options = [
        [
            'label' => "普通订阅号",
            'value' => "1"
        ],
        [
            'label' => "普通服务号",
            'value' => "2"
        ],
        [
            'label' => "认证订阅号",
            'value' => "3"
        ],
        [
            'label' => "认证服务号/认证媒体/政府订阅号",
            'value' => "4"
        ],
    ];
    /**
     * @var PlatformWechat
     */
    public $model;
    /**
     * @var Platform
     */
    public $PlatformModel;

    public function __construct(App $app, PlatformWechat $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 显示微信平台列表
     * @Route("wechat", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        //
        return $this->success("ok");
    }

    /**
     * 显示创建微信平台表单页.
     * @Route("wechat/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        $rule = array();
        //创建表单
        $form = Form::createForm("/adminapi/platform/wechat/index")->setMethod("POST");
        //新增表单
        $rule[] = Form::select('level', '类型')->options($this->model->level_options)->required();
        $rule[] = Form::input('sort', '排序', "0");
        $rule[] = Form::input('name', '公众号名称', "")->required();
        $rule[] = Form::input('description', '描述', "");
        $rule[] = Form::dkImage('logo', '公众号图标', "");
        $rule[] = Form::input('original', '原始id', "");
        $rule[] = Form::input('appid', 'AppId', "")->required();
        $rule[] = Form::input('secret', 'AppSecret', "")->required();
        $form->setTitle("新增微信公众平台")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存新建的微信平台
     * @Route("wechat", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $wallet = $this->userInfo['wallet'];
        if ($wallet['wechat'] <= $wallet['use_count']['wechat']) {
            return $this->error("创建次数不足");
        }
        $formData = $request->post('formData', []);
        $platform = new Platform();
        $platform_data = array(
            "type"        => 1,
            "uid"         => $this->userInfo['uid'],
            "name"        => $formData['name'],
            "description" => $formData['description'],
            "logo"        => $formData['logo'],
            "sort"        => $formData['sort'],
        );
        $platform->save($platform_data);
        $wechat_data = array(
            "plaid"    => $platform->plaid,
            "level"    => $formData['level'],
            "name"     => $formData['name'],
            "original" => $formData['original'],
            "appid"    => $formData['appid'],
            "secret"   => $formData['secret'],
        );
        $this->model->save($wechat_data);
        return $this->success("新增成功", ['plaid' => $platform->plaid]);
    }

    /**
     * 显示指定的微信平台
     * @Route("wechat/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑微信平台表单页.
     * @Route("wechat/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $rule = array();
        //创建表单
        $platform = new Platform();
        $row = $platform->with("wechat")->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("当前平台不存在");
        }
        $form = Form::createForm("/adminapi/platform/wechat/index/" . $id)->setMethod("PUT");
        //新增表单
        $rule[] = Form::select('level', '类型', (string)$row->wechat->level)->options($this->model->level_options)->required();
        $rule[] = Form::input('sort', '排序', $row->sort);
        $rule[] = Form::input('name', '公众号名称', $row->name)->required();
        $rule[] = Form::input('description', '描述', $row->description);
        $rule[] = Form::dkImage('logo', '公众号图标', $row->logo)->appendToBody();
        $rule[] = Form::input('original', '原始id', $row->wechat->original);
        $rule[] = Form::input('appid', 'AppId', $row->wechat->appid, '')->required();
        $rule[] = Form::input('secret', 'AppSecret', $row->wechat->secret)->required();
        $form->setTitle("编辑微信公众平台")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存更新的微信平台
     * @Route("wechat/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->post('formData', []);
        $wechat = new PlatformWechat();
        $platform = new Platform();

        $wechat_data = $wechat->where(['plaid' => $id])->find();
        $platform_data = $platform->where(['plaid' => $id])->find();
        if (!$wechat_data || !$platform_data) {
            return $this->error("当前公众平台不存在");
        }
        $platform_data->save([
            "type"        => 1,
            "name"        => $formData['name'],
            "description" => $formData['description'],
            "logo"        => $formData['logo'],
            "sort"        => $formData['sort'],
        ]);
        $wechat_data->save([
            "level"    => $formData['level'],
            "name"     => $formData['name'],
            "original" => $formData['original'],
            "appid"    => $formData['appid'],
            "secret"   => $formData['secret'],
        ]);
        return $this->success("编辑成功");
    }

    /**
     * 删除指定微信平台
     * @Route("wechat/id", method="DELETE")
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }


    public function getWechatMessage()
    {
        $plaid = input('plaid', 0);
        if (!$plaid) {
            return $this->error('平台ID不存在');
        }
        $res = $this->model->getPlatformDetails($plaid);
        if (!$res) {
            return $this->error('平台不存在');
        }
        $data = array(
            'api_url'        => $res['api_url'],
            'token'          => $res['token'],
            'encodingaeskey' => $res['encodingaeskey']
        );
        return $this->success('ok', $data);
    }

    public function editWechatMessage()
    {
        $formData = $this->request->post('formData', []);
        $plaid = input('plaid', 0);
        $wechat = new PlatformWechat();

        $wechat_data = $wechat->where(['plaid' => $plaid])->find();
        if (!$wechat_data) {
            return $this->error("当前公众平台不存在");
        }
        $wechat_data->save([
            "token"          => $formData['token'],
            "encodingaeskey" => $formData['encodingaeskey'],
        ]);
        return $this->success("修改成功");
    }
}
